package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;

import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrderAdminMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.result.PageResult;
import com.sky.service.OrderAdminService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class OrderAdminServiceImpl implements OrderAdminService {

    @Autowired
    private OrderAdminMapper orderAdminMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 接单
     */
    @Override
    public void receiveOrder(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = new Orders();
        orders.setStatus(Orders.CONFIRMED);
        orders.setId(ordersConfirmDTO.getId());
        orderMapper.updateOrderStatus(orders);
    }

    /**
     * 搜索订单业务
     * @param ordersPageQueryDTO   前端传过来的
     * @return
     */
    @Override
    public PageResult searchOrder(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Page<Orders> ordersPage = orderAdminMapper.selectOrderByDTO(ordersPageQueryDTO);
        PageResult pageResult = new PageResult(ordersPage.getTotal(), ordersPage.getResult());
        return pageResult;
    }
    // 各个状态的订单数量统计
    @Override

    public OrderStatisticsVO statistics() {
        //查询出待接单、待派送、派送中的订单数量
        //待接单
        Integer toBeConfirmed = orderAdminMapper.countStatus(Orders.TO_BE_CONFIRMED);
        //待派送
        Integer comfirmed = orderAdminMapper.countStatus(Orders.CONFIRMED);
        //派送中
        Integer deliveryInProgress = orderAdminMapper.countStatus(Orders.DELIVERY_IN_PROGRESS);

        //查询结果 封装到OrderStatisticsVO
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
        orderStatisticsVO.setConfirmed(comfirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
        return orderStatisticsVO;
    }

    public OrderVO selectOrder(Long id) {
        log.info("业务层") ;
        OrderVO orderVo = new OrderVO();
        List<OrderDetail> orderVOList = orderDetailMapper.selectOrderDetailByOrderId(id);
        Orders orders = orderAdminMapper.selectOrder(id);
        BeanUtils.copyProperties(orders,orderVo);
        orderVo.setOrderDetailList(orderVOList);
        return orderVo;

    }

    //商家取消订单
    @Override
    public void cancle(OrdersCancelDTO ordersCancelDTO) {
        // 根据id查询订单
        Orders ordersDB = orderAdminMapper.getById(ordersCancelDTO.getId());
        //支付状态
        if (ordersDB.getPayStatus() == Orders.PAID) {
            log.info("[给用户退款]");
        }

        Orders orders = new Orders();
        orders.setId(ordersCancelDTO.getId());
        orders.setStatus(Orders.CANCELLED);//以取消
        orders.setCancelReason(ordersCancelDTO.getCancelReason());
        orders.setCancelTime(LocalDateTime.now());
        orderMapper.update(orders);
    }

    /**
     * 拒单业务
     * @param ordersRejectionDTO
     */
    @Override
    public void rejectOrder(OrdersRejectionDTO ordersRejectionDTO) {
        Orders queryOrder = orderAdminMapper.selectOrderById(ordersRejectionDTO.getId());
        if (queryOrder == null && queryOrder.getStatus() != Orders.TO_BE_CONFIRMED) {
            return;
        }
        //根据订单id更新订单状态、拒单原因、取消时间
        Orders orders = new Orders();
        orders.setId(queryOrder.getId());
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelReason(queryOrder.getRejectionReason());
        orders.setCancelTime(LocalDateTime.now());
        orderMapper.updateOrderStatus(orders);
    }

    //派送订单
    @Override
    public void delivery(Long id) {
        // 根据id查询订单
        Orders ordersDB = orderAdminMapper.getById(id);

        // 校验订单是否存在，并且状态为已结单状态3
        if (ordersDB == null || !ordersDB.getStatus().equals(Orders.CONFIRMED)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }

        Orders orders = new Orders();
        orders.setId(ordersDB.getId());
        // 更新订单状态,状态转为派送中
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderMapper.update(orders);
    }

    @Override
    public void complete(Long id) {
        // 根据id查询订单
        Orders ordersDB = orderMapper.getById(id);

        // 校验订单是否存在，并且状态为4
        if (ordersDB == null || !ordersDB.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }

        Orders orders = new Orders();
        orders.setId(ordersDB.getId());
        // 更新订单状态,状态转为完成
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());

        orderMapper.update(orders);
    }
}
